let app = getApp();
Page({
  data: {
    page: 1,//页数
    pages: 0,//总页数
    childrenIfy: [],
    keyname:'',
    indicatorDots: true,
    vertical: true,
    autoplay: true,
    interval: 4000,
    duration: 1500,
    navData: [{
      name: "首页",
      current: 0,
      style: 0,
      ico: 'icon-deng',
      fn: 'navJumpPage',      
      id: '/pages/home/home'
    }, {
      name: "分类",
      current: 1,
      style: 0,
      ico: 'icon-fenlei',
      fn: 'navJumpPage',
      id: ''
    }, {
      name: "购物车",
      current: 0,
      style: 0,
      ico: 'icon-gouwuche01',
      fn: 'navJumpPage',
      id: '/pages/cart/cart'
    }, {
      name: "我的",
      current: 0,
      style: 0,
      ico: 'icon-dkw_wode',
      fn: 'navJumpPage',
      id: '/pages/mine/mine'
    }],
    cateItems: [],
    curNav: 4,
    curIndex: 0
  },
  onShow() {   
    let that = this;
    app.get('GetAllShangPinFenLei').then((res) => {
      that.setData({
        cateItems: res
      });
    });
    this.ifyAjax(that.data.curNav);
  },
  onLoad(){
    this.ClassGetLunBo();
  },
  navJumpPage(event) {
    let url = event.currentTarget.id;
    if (url) {
      wx.redirectTo({
        url: url,
      });
    }
  },
  inputConfirm(e){
    this.setData({
      keyname: e.detail.value
    });
    this.setData({
      page: 1,
    });
    this.ifyAjax(this.data.curNav, 'left');
  },
  //事件处理函数 
  switchRightTab(e) {
    // 获取item项的id，和数组的下标值 
    let id = e.target.dataset.id,
      that = this,
      index = parseInt(e.target.dataset.index);
    // 把点击到的某一项，设为当前index 
    this.setData({
      curNav: id,
      page:1,
      curIndex: index
    });
    this.ifyAjax(id,'left');
  },
  ifyAjax(id,flag){
    let that =this;
    wx.showLoading();
    app.get('GetShangPinNew', {
      action: 'getbypinpai',
      yema: that.data.page,
      keyname: that.data.keyname,
      PinPaiLeiBieID: id
    }).then((res) => {
      wx.hideLoading();      
      if (flag==='left'){
        that.setData({
          childrenIfy: res.data,
          pages: Math.round((res.zongtiaoshu) / 10)
        })
      }else{
        that.setData({
          childrenIfy: that.data.childrenIfy.concat(res.data),
          pages: Math.round((res.zongtiaoshu) / 10)
        })
      }
    })
  },
  ClassGetLunBo(){
    let that = this;
    app.get('ClassGetLunBo',{
      action: 'getpinpaihaibao'
    }).then((res)=>{
      that.setData({
        indexImg:res
      })
    })
  }, 
  scrollEvent(event){
    // 上拉刷新
    let pageNumber = this.data.page,
      pagesNumber = this.data.pages;
    pageNumber++;
    this.setData({
      page: pageNumber
    });
    if ((pageNumber > pagesNumber) && pagesNumber > 0) {
      return app.toast('没有更多数据');
    }
    this.ifyAjax(this.data.curNav);
  }
})